package main

import (
	"fmt"
	"strings"
)

func main() {
	f := addUpper()
	fmt.Println(f(1)) //11
	fmt.Println(f(2)) //13
	fmt.Println(f(3)) //16

	f2 := makeSuffix(".jpg")
	fmt.Println(f2("t1.jpg"))
	fmt.Println(f2("t2"))
}

func addUpper() func(int32) int32 {
	var n int32 = 10
	return func(x int32) int32 {
		n = n + x
		return n
	}
}

//闭包的最佳实践
func makeSuffix(suffix string) func(string) string {
	return func(name string) string {
		if !strings.HasSuffix(name, suffix) {
			return name + suffix
		}
		return name
	}
}
